package com.wtp.wallpaper.rabbitmq.config;

import com.wtp.wallpaper.rabbitmq.consumer.RatingMessageConsumer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class RabbitMQConsumerConfig {

    @Autowired
    private ConnectionFactory connectionFactory;

    // 配置 SimpleMessageListenerContainer 用于控制 RabbitMQ 消费者的启停
    @Bean
    public SimpleMessageListenerContainer messageListenerContainer(MessageListenerAdapter listenerAdapter) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames("rating.queue");//监听哪个队列
        container.setMessageListener(listenerAdapter);//绑定消费者的方法
        return container;
    }

    // 配置 MessageListenerAdapter，将消息发送到消费者的方法
    @Bean
    public MessageListenerAdapter listenerAdapter(RatingMessageConsumer consumer) {
        //log.info("66666==={}",consumer);
        //哪个消费者的哪个方法
        return new MessageListenerAdapter(consumer, "receiveRatingMessage");
    }
}
